<template>
    <div>
        <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>生产物料控制</el-breadcrumb-item>
        <el-breadcrumb-item>物料维护</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
        <el-row :gutter="10">
            <el-col :span="7">
                <el-input placeholder="请输入物料编号检索" v-model="query">
                    <template #append>
                        <el-button icon="el-icon-search" @click="getList">检索</el-button>
                    </template>
                </el-input>
            </el-col>
            <el-col span="4">
                <el-button type="primary" icon="el-icon-plus" @click="addDialogVisible = true">新增</el-button>
            </el-col>
        </el-row>
        <!-- 表格区域 -->
        <el-table stripe border
        ref="multipleTable"
        :data="itemList"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange">
    <el-table-column prop="id" label="id" width="120" align="center">
    </el-table-column>
    <el-table-column prop="item_no" label="物料编号" width="200" align="center">
    </el-table-column>
    <el-table-column prop="item_name" label="物料名称" width="200" align="center">
    </el-table-column>
    <el-table-column prop="item_desc" label="物料描述" width="200" align="center">
    </el-table-column>
    <el-table-column prop="bom_no" label="物料BOM编号" width="240" align="center">
    </el-table-column>
    <el-table-column prop="item_type" label="物料类型" width="190" align="center">
      <template #default="scope">
            <span v-if="scope.row.item_type=='purchase'">外采购</span>
            <span v-if="scope.row.item_type=='machining'">自生产</span>
      </template>
    </el-table-column>
    <el-table-column prop="site" label="挂靠站点" width="100" align="center">
    </el-table-column>
    <el-table-column prop="by_user" label="创建人" width="120" align="center">
    </el-table-column>
    <el-table-column prop="create_time" label="创建时间" width="240" align="center">
      <template #default="scope">
            {{formatDate(scope.row.create_time)}}
      </template>
    </el-table-column>
    <el-table-column label="操作" width="120" align="center" fixed="right">
      <template #default="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="editDialog(scope.row)"></el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="del(scope.row)"></el-button>
      </template>
    </el-table-column>
  </el-table>
  <!-- 分页区域 -->
   <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNow"
      :page-sizes="[1, 2, 5, 10]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="rowCount">
    </el-pagination>
    </el-card>
    <!-- 添加对话框 -->
    <el-dialog title="新增物料" v-model="addDialogVisible" width="40%">
      <!-- 新增物料BOM二级对话框 -->
      <el-dialog
        title="物料BOM列表"
        v-model="BOMDV"
        width="40%"
        append-to-body
      >
        <!-- 对话框内容 -->
        <el-table
          stripe
          border
          ref="multipleTable"
          :data="BOMList"
          tooltip-effect="dark"
          style="width: 100%"
          @selection-change="handleSelectionChange"
          size="small"
        >
          <el-table-column prop="id" label="id" width="60" align="center">
          </el-table-column>
          <el-table-column
            prop="item_bom_no"
            label="物料BOM编号"
            width="180"
            align="center"
            show-overflow-tooltip
          >
          </el-table-column>
          <el-table-column
            prop="item_bom_name"
            label="物料BOM名称"
            width="180"
            align="center"
            show-overflow-tooltip
          >
          </el-table-column>
          <el-table-column
            prop="item_bom_desc"
            label="物料BOM描述"
            width="180"
            align="center"
            show-overflow-tooltip
          >
          </el-table-column>
          <el-table-column label="操作" width="auto" align="center">
            <template #default="scope">
              <el-button size="mini" @click="addBOM(scope.row)"
                >选择</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页区域 -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNow"
          :page-sizes="[1, 2, 5, 10]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="rowCount"
        >
        </el-pagination>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="BOMDV = false">取 消</el-button>
          </span>
        </template>
      </el-dialog>
      <!-- 二级对话框结束 -->
      <!-- 对话框内容 -->
      <el-form
        :model="addForm"
        status-icon
        :rules="addFormrules"
        ref="addFormRef"
        label-width="120px"
      >
        <el-form-item label="站点" prop="site">
          <el-input v-model="addForm.site" disabled placeholder="请选择站点"></el-input>
        </el-form-item>
        <el-form-item label="物料编号" prop="item_no">
          <el-input v-model="addForm.item_no" placeholder="请输入物料编号"></el-input>
        </el-form-item>
        <el-form-item label="物料名称" prop="item_name">
          <el-input v-model="addForm.item_name" placeholder="请输入物料名称"></el-input>
        </el-form-item>
        <el-form-item label="物料描述" prop="item_desc">
          <el-input v-model="addForm.item_desc" placeholder="请输入物料描述"></el-input>
        </el-form-item>
        <el-form-item label="物料BOM" prop="bom_no">
          <el-input v-model="addForm.bom_no" placeholder="请选择物料BOM">
          <template #suffix>
          <i class="el-input__icon el-icon-search" @click="addbom"></i>
          </template>
          </el-input>
        </el-form-item>
        <el-form-item label="物料类型" prop="item_type">
          <el-select
            v-model="addForm.item_type"
            placeholder="请选择类型"
          >
            <el-option label="自生产" value="machining"></el-option>
            <el-option label="外采购" value="purchase"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否可用" prop="state">
          <el-select
            v-model="addForm.state"
            placeholder="请选择是否可用"
          >
            <el-option label="是" value="1"></el-option>
            <el-option label="否" value="0"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="addDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="add" 
            >确 定</el-button
          >
        </span>
      </template>
    </el-dialog>
    <!-- 编辑对话框 -->
    <el-dialog title="编辑物料" v-model="editDialogVisible" width="40%">
      <!-- 新增物料BOM二级对话框 -->
      <el-dialog
        title="物料BOM列表"
        v-model="BOMDV"
        width="40%"
        append-to-body
      >
        <!-- 对话框内容 -->
        <el-table
          stripe
          border
          ref="multipleTable"
          :data="BOMList"
          tooltip-effect="dark"
          style="width: 100%"
          @selection-change="handleSelectionChange"
          size="small"
        >
          <el-table-column prop="id" label="id" width="60" align="center">
          </el-table-column>
          <el-table-column
            prop="item_bom_no"
            label="物料BOM编号"
            width="180"
            align="center"
            show-overflow-tooltip
          >
          </el-table-column>
          <el-table-column
            prop="item_bom_name"
            label="物料BOM名称"
            width="180"
            align="center"
            show-overflow-tooltip
          >
          </el-table-column>
          <el-table-column
            prop="item_bom_desc"
            label="物料BOM描述"
            width="180"
            align="center"
            show-overflow-tooltip
          >
          </el-table-column>
          <el-table-column label="操作" width="auto" align="center">
            <template #default="scope">
              <el-button size="mini" @click="editBOM(scope.row)"
                >选择</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页区域 -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNow"
          :page-sizes="[1, 2, 5, 10]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="rowCount"
        >
        </el-pagination>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="BOMDV = false">取 消</el-button>
          </span>
        </template>
      </el-dialog>
      <!-- 二级对话框结束 -->
      <!-- 编辑框内容 -->
      <el-form
        :model="editForm"
        status-icon
        :rules="addFormrules"
        ref="editFormRef"
        label-width="120px"
      >
        <el-form-item label="站点" prop="site">
          <el-input v-model="editForm.site" disabled placeholder="请选择站点"></el-input>
        </el-form-item>
        <el-form-item label="物料编号" prop="item_no">
          <el-input v-model="editForm.item_no" placeholder="请输入物料编号"></el-input>
        </el-form-item>
        <el-form-item label="物料名称" prop="item_name">
          <el-input v-model="editForm.item_name" placeholder="请输入物料名称"></el-input>
        </el-form-item>
        <el-form-item label="物料描述" prop="item_desc">
          <el-input v-model="editForm.item_desc" placeholder="请输入物料描述"></el-input>
        </el-form-item>
        <el-form-item label="物料BOM" prop="bom_no">
          <el-input v-model="editForm.bom_no" placeholder="请选择物料BOM">
          <template #suffix>
          <i class="el-input__icon el-icon-search" @click="editbom"></i>
          </template>
          </el-input>
        </el-form-item>
        <el-form-item label="物料类型" prop="item_type">
          <el-select
            v-model="editForm.item_type"
            placeholder="请选择类型"
          >
            <el-option label="自生产" value="machining"></el-option>
            <el-option label="外采购" value="purchase"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否可用" prop="state">
          <el-select
            v-model="editForm.state"
            placeholder="请选择是否可用"
          >
            <el-option :label="是" value="1"></el-option>
            <el-option :label="否" value="0"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="editDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="edit" 
            >确 定</el-button
          >
        </span>
      </template>
    </el-dialog>
    </div>
</template>

<script>
import axios from 'axios'
export default {
  data () {
    return {
      itemList:[],
      query:'',
      pageCount:1,
      pageNow:1,
      pageSize:10,
      rowCount:0,
      // 显示添加对话框
      addDialogVisible: false,
      // 显示编辑对话框 
      editDialogVisible: false,
      //显示物料BOM二级弹出框
      BOMDV:false,
      addForm: {
        site: "*",
        item_no: "",
        item_name: "",
        item_desc: "",
        bom_no: "",
        item_type: "",
        state: "",
      },
      BOMList:[],
      editForm:{},
      addFormrules: {
        station: [],
        item_no: [
          {
            required: true,
            message: "请输入物料编号",
            trigger: "blur",
          },
        ],
        item_name: [
          {
            required: true,
            message: "请输入物料名称",
            trigger: "blur",
          },
        ],
        item_desc: [
          {
            required: true,
            message: "请输入物料描述",
            trigger: "blur",
          },
        ],
        bom_no: [
          {
            required: true,
            message: "请输入物料BOM",
            trigger: "blur",
          },
        ],
        item_type: [
          {
            required: true,
            message: "请选择物料类型",
            trigger: "blur",
          },
        ],
        state: [
          {
            required: true,
            message: "请选择是否可用",
            trigger: "blur",
          },
        ],
      },
    }
  },
  mounted () {
    this.getList()
  },
  methods: {
    getList() {
      this.$axios({
        headers: {
              "Content-Type":
                "application/x-www-form-urlencoded; charset=UTF-8",
            },
        method: "post",
        url: "/IE_MES/item_resource/item_findByPage.shtml",
        data: this.$qs.stringify({
              "pageSize":this.pageSize,
              "pageNow": this.pageNow,
              "itemFormMap.item_no": this.query
            }),
      })
        .then((res) => {
          console.log(res.data);
          this.itemList = res.data.records;
          this.pageCount = res.data.pageCount;
          this.pageNow = res.data.pageNow;
          this.pageSize = res.data.pageSize;
          this.rowCount = res.data.rowCount;
        });
    },
     // 新增
    add() {
      this.$axios({
            headers: {
              "Content-Type":
                "application/x-www-form-urlencoded; charset=UTF-8",
            },
            method: "post",
            url: "/IE_MES/item_resource/item_addEntity.shtml",
            data: this.$qs.stringify({
              "itemFormMap.site":this.addForm.site,
              "itemFormMap.item_no":this.addForm.item_no,
              "itemFormMap.item_name":this.addForm.item_name,
              "itemFormMap.item_desc":this.addForm.item_desc,
              "itemFormMap.bom_no":this.addForm.bom_no,
              "itemFormMap.item_type":this.addForm.item_type,
              "itemFormMap.state":this.addForm.state,
            }),
      }).then((res) => {
            if (res.data == "{\"result\":\"success\",\"score\":\"\"}") {
              this.getList();
              this.$refs['addFormRef'].resetFields();
              this.addDialogVisible=false;
              this.$message({
                type: "success",
                message: "添加成功!",
              });
            }
          });
    },
    addbom() {
      this.BOMDV = true;
      this.$axios({
        headers: {
          "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
        },
        method: "post",
        url: "/IE_MES/item_resource/bom_findByPage.shtml",
        data: this.$qs.stringify({
          pageSize: this.pageSize,
          pageNow: this.pageNow,
        }),
      }).then((res) => {
        console.log(res.data);
        this.BOMList = res.data.records;
        this.pageCount = res.data.pageCount;
        this.pageNow = res.data.pageNow;
        this.pageSize = res.data.pageSize;
        this.rowCount = res.data.rowCount;
      });
    },
    addBOM(row){
      this.addForm.bom_no = row.item_bom_no;
      this.BOMDV = false;
    },
    // 编辑
    editDialog(row) {
      this.editForm = JSON.parse(JSON.stringify(row));
      console.log(JSON.parse(JSON.stringify(row)));
      this.editDialogVisible = true;
    },
    edit() {
      this.$axios({
            headers: {
              "Content-Type":
                "application/x-www-form-urlencoded; charset=UTF-8",
            },
            method: "post",
            url: "/IE_MES/item_resource/item_editEntity.shtml",
            data: this.$qs.stringify({
              "itemFormMap.id":this.editForm.id,
              "itemFormMap.site":this.editForm.site,
              "itemFormMap.item_no":this.editForm.item_no,
              "itemFormMap.item_name":this.editForm.item_name,
              "itemFormMap.item_desc":this.editForm.item_desc,
              "itemFormMap.bom_no":this.editForm.bom_no,
              "itemFormMap.item_type":this.editForm.item_type,
              "itemFormMap.state":this.editForm.state,
            }),
      }).then((res) => {
            if (res.data == "success") {
              this.getList();
              this.editDialogVisible=false;
              this.$message({
                type: "success",
                message: "编辑成功!",
              });
            }
          });
    },
    editbom() {
      this.BOMDV = true;
      this.$axios({
        headers: {
          "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
        },
        method: "post",
        url: "/IE_MES/item_resource/bom_findByPage.shtml",
        data: this.$qs.stringify({
          pageSize: this.pageSize,
          pageNow: this.pageNow,
        }),
      }).then((res) => {
        console.log(res.data);
        this.BOMList = res.data.records;
        this.pageCount = res.data.pageCount;
        this.pageNow = res.data.pageNow;
        this.pageSize = res.data.pageSize;
        this.rowCount = res.data.rowCount;
      });
    },
    editBOM(row){
      this.editForm.bom_no = row.item_bom_no;
      this.BOMDV = false;
    },
    // 删除
    del(row) {
      this.$confirm("此操作将永久删除该巡检内容, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$axios({
            headers: {
              "Content-Type":
                "application/x-www-form-urlencoded; charset=UTF-8",
            },
            method: "post",
            url: "/IE_MES/item_resource/item_deleteEntity.shtml?_t="+new Date(),
            data: this.$qs.stringify({
              ids:row.id
            }),
          }).then((res) => {
            if (res.data == "success") {
              this.getList();
              this.$message({
                type: "success",
                message: "删除成功!",
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleSizeChange (newSize) {
      this.pageSize = newSize
      this.getList()
    },
    handleCurrentChange (newPage) {
      this.pageNow = newPage
      this.getList()
    },
    formatDate(data) {
        var date = new Date(data);
        var YY = date.getFullYear() + '-';
        var MM = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
        var DD = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate());
        var hh = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
        var mm = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
        var ss = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
        return YY + MM + DD + " " + hh + mm + ss;
    },
  }
}
</script>

<style lang="less" scoped>
.el-form-item /deep/ .el-input__inner {
  padding-right: 420px !important;
}
</style>
